<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022/8/17
  Time: 10:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <base href="${pageContext.request.contextPath}/">
    <title>添加</title>
</head>
<body>
<form action="AddServlet" method="post">
    <table align="center" border="1px">
        <tr>
            <td colspan="2"><h3>添加组织机构信息</h3></td>
        </tr>
        <tr>
            <td>组织机构名称(*)</td>
            <td><input type="text" id="name" name="name"><br><span id="span"></span></td>
        </tr>
        <tr>
            <td>负责人(*)</td>
            <td><input type="text" id="principalName" name="principalName"></td>
        </tr>
        <tr>
            <td>创建日期(*)</td>
            <td><input type="text" id="createDate" name="createDate"></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit"> <input type="button" onclick="location.href='ListServlet'" value="返回"></td>
        </tr>
    </table>
</form>
<script src="jq/jquery.js"></script>
<script>

    $('form').submit(function () {
        var flag=true;
        if(!CheckName()) flag=false;
        else if(!CheckIdPrincipalName()) flag=false;
        else if(!CheckCreateDate()) flag=false;
        return flag;
    })

    var a = true;

    function CheckName() {
        var name = $("#name").val();
        $("#span").text("");
        if(name==""){
            alert("请填写完整的组织机构信息！")
            return false;
        }else{
            $.getJSON(
                "NameServlet",
                {name:name},
                function (data) {
                    if (data) {
                        $('#span').text("该组织机构已存在");
                        a=false;
                    }
                }
            )
            return a;
        }
        return true;
    }

    function CheckIdPrincipalName() {
        if($('#name').val()==""){
            alert("请填写完整的组织机构信息！")
            return false;
        }
        return true;
    }

    function CheckCreateDate() {
        var cerateDate = $('#createDate').val();
        var date =/^(\d{4})-(\d{2})-(\d{2})$/;
        if(cerateDate==""){
            alert("请填写完整的组织机构信息！")
            return false;
        }else if(date.test(cerateDate) == false){
            alert("日期格式不正确！")
            return false;
        }
        return true;
    }


    function name() {
        var name = $("#name").val();
        $("#span").text("");
        $.getJSON(
            "AddServlet",
            {name:name},
            function (data) {
                if (data) {
                    $('#span').text("该组织机构已存在");
                    return false;
                }else{
                    return true;
                }
            }
        )
    }
</script>
</body>
</html>
